#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include "typedef.h"
#include "common.h"
#include "timerctrl.h"

static TIMER_LISTEN_CALLBACK_FUNC user_func = NULL;
static int interval;
static int flag;

static void timer_callback_function();

int tmrctrl_start(TIMER_LISTEN_CALLBACK_FUNC func, int time)
{
	if (func == NULL || time <= 0)
		return ERROR;

	user_func = func;
	interval  = time;

	flag = TRUE;

	signal(SIGALRM, timer_callback_function);
	alarm(interval);

	return SUCCESS;
}

void tmrctrl_stop()
{
	flag = FALSE;
}

void timer_callback_function()
{
	if ( user_func )
	{
		user_func();
	}

	if (flag)
	{
		signal(SIGALRM, timer_callback_function);
		alarm(interval);
	}
}

